<?php

class Admin_CategoryController extends Zend_Controller_Action
{

    public function init()
    {
        $this->_auth = Zend_Auth::getInstance();
    	
        if(!$this->_auth->hasIdentity()) {
        	$this->_redirect($this->view->adminURL('authentication', 'login'));
        }
        $this->view->user = $this->_auth->getIdentity();
		
		$this->_category_model = new Admin_Model_DbTable_Category();
		$this->_user_model = new Admin_Model_DbTable_User();
		$this->_post_model =  new Admin_Model_DbTable_Post();
    }

    public function indexAction()
    {
        // action body
    }

    public function listAction()
    {
		$this->view->q = $this->_request->getParam('q');
		//$this->view->c = $this->_request->getParam('cate');
			
		$select = $this->_category_model->select()
		->order('cat_id desc');
		
		if ($this->view->q)
			$select = $select->where('cat_title like ?', '%'.$this->view->q.'%');
		//if ($this->view->c)
		//	$select = $select->where('cat_id = ?', $this->view->c);
		
		$this->view->users = $this->_user_model->filter($this->_user_model->fetchAll()->toArray());
		$all_cate = $this->_category_model->fetchAll($select)->toArray();
		
		$this->view->total = count($all_cate);
		$this->view->categories = $all_cate;
		
		$this->view->headTitle('Danh sách danh mục');
    }

    public function addAction()
    {
        // action body
    }
	
    public function editAction()
    {
		$request = $this->getRequest();
		$id = intval($request->getParam('id'));
		if (!$id) {
			throw new Zend_Controller_Action_Exception('This page does not exist', 404);
		}
		
		//Change post if form submited
		if($this->getRequest())
		if ($this->getRequest()->isPost()) {
			
			$this->_category_model->update(array(
				'cat_title' => $request->getPost('cat_title'),
				'cat_alias' => $request->getPost('cat_alias'),
			), "cat_id='{$id}'");
			$this->view->info = "Updated.";
		}
		
		
		$form = new Admin_Form_EditCate();
		$result_cate = $this->_category_model->fetchAll(
			$this->_category_model->select()
			->where("cat_id='{$id}'")
		);
		
		$result_cate = $result_cate[0];
		$this->view->cate = $result_cate;
		$this->view->headTitle("Chỉnh sửa ". $result_cate->cat_title);
		
		//Change form default value
		$form->id->setValue($result_cate->cat_id);
		$form->cat_title->setValue($result_cate->cat_title);
		$form->cat_alias->setValue($result_cate->cat_alias);
		$form->Create->setLabel( 'Lưu thay đổi');
		
		$this->view->form = $form;
		
    }

    public function deleteAction()
    {
        $id = $this->getRequest()->getParam('id');
        $this->_model->delete('id = '.$id);
		die();
    }
	
	public function changeAction(){
		// print_r($this->_request->getParams());
		$request = $this->getRequest();
		if ($this->getRequest()->isPost()) {
			$id = $request->getPost('cat_id');
			$this->_category_model->update(array(
				'cat_title' => $request->getPost('cat_title'),
				// 'cat_alias' => $request->getPost('cat_alias'),
			), "cat_id='{$id}'");
			$this->view->info = "Updated.";
		}
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
	}

	
	
	public function fixaliasAction () {
		$model = new Application_Model_DbTable_Post();
		
		$all = $model->fetchAll();
		
		foreach($all as $post) {
			$alias = $this->view->getAlias($post->title);
			$model->update(array(
				'alias' => $alias
			), "id=".$post->id);
			print "<br />";
		}
		
		
		
		
		
		
		
		
		
		
		die();
	}
}







